package com.live.dengdengtv.exception;

import java.lang.Thread.UncaughtExceptionHandler;


import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.live.dengdengtv.activity.ScreenOrientationActivity;

public class CustomException implements UncaughtExceptionHandler {

	// 获取application 对象；
	private Context mContext;

	private UncaughtExceptionHandler defaultExceptionHandler;
	// 单例声明CustomException;
	private static CustomException customException;

	private CustomException() {
	}

	public static CustomException getInstance() {
		if (customException == null) {
			customException = new CustomException();
		}
		return customException;
	}

	@Override
	public void uncaughtException(Thread thread, Throwable exception) {
		if (defaultExceptionHandler != null) {

			Log.e("mobilebiz->exception", exception.getLocalizedMessage());
			exception.printStackTrace();
			// 将异常抛出，则应用会弹出异常对话框.这里先注释掉
			// defaultExceptionHandler.uncaughtException(thread, exception);
			for (Activity activity : ScreenOrientationActivity.sAllActivity) {
				activity.finish();
			}
			ScreenOrientationActivity.sAllActivity.clear();
			System.exit(10);
		}
	}

	public void init(Context context) {
		mContext = context;
		defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
		Thread.setDefaultUncaughtExceptionHandler(this);

	}

}
